package cn.lyq.card.ui.wallet

import android.content.Context
import android.content.Intent
import android.text.Html
import android.view.View
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import cn.lyq.card.R
import cn.lyq.card.base.BaseActivity
import cn.lyq.card.data.model.WalletInfo
import cn.lyq.card.manager.UserManager
import cn.lyq.card.utils.setClick
import cn.lyq.card.utils.toBankNo
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.act_cash_msg.*
import kotlinx.android.synthetic.main.layout_cash_msg.*
import kotlinx.android.synthetic.main.layout_title.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


class CashMsgTempActivity : BaseActivity() {

    companion object {
        private const val KEY_INFO = "INFO"

        @JvmStatic
        fun start(context: Context, info: WalletInfo) {
            val starter = Intent(context, CashMsgTempActivity::class.java)
                .putExtra(KEY_INFO, info)
            context.startActivity(starter)
        }
    }

    private var selectedIndex: Int = 0
    private val ids = arrayOf(R.id.s1, R.id.s2, R.id.s3)
    private val mInfo by lazy {
        intent.getSerializableExtra(KEY_INFO)
            ?.takeIf { it is WalletInfo }
            ?.run { this as WalletInfo }
    }

    override fun getLayoutId(): Int {
        return R.layout.act_cash_msg_temp
    }

    override fun initView() {
        super.initView()
        setMyTitle(R.string.str_cash_quickly)
        tvTitleRight.setTextColor(ContextCompat.getColor(this, R.color.font_main))
        tvTitleRight.setText(R.string.str_cash_record)
        tvTitleRight.visibility = View.VISIBLE

        tvCardNo.text = UserManager.bankCardInfo?.cardNumber?.toBankNo() ?: ""
        tvCardName.text = UserManager.instance.value?.trueName ?: ""
        tvCashTip.text = Html.fromHtml(
            getString(R.string.str_cash_detail_tip)
        )

        mInfo?.let {

        }
    }

    override fun initListener() {
        super.initListener()
        lifecycleScope.launch(Dispatchers.Main) {
            while (true) {
                delay(1000L)
                val first = selectedIndex
                if (++selectedIndex == 3) {
                    selectedIndex = 0
                }
                motionBanner.setTransition(ids[first], ids[selectedIndex])
                motionBanner.transitionToEnd()
            }
        }
        tvConfirm.setClick {



            val money = etCashMoney.text.trim().toString().toDoubleOrNull()
            if (money == null) {
                ToastUtils.showShort(etCashMoney.hint)
                return@setClick
            }
            setBarColor(true)
            if (UserManager.instance.value?.hasCashPwd() == true) {
                CashPwdFg.newInstance(money, banner.currentItem).show(this)
            } else {
                CashPwdUnsetFg().show(this)
            }
        }
        tvTitleRight.setClick {
            startActivity(CashRecordActivity::class.java)
        }
    }

    override fun initObserver() {
        super.initObserver()
    }
}